package com.ourway.controller;

import cn.hutool.core.util.StrUtil;
import com.liuweiheng.base.entity.R;
import com.liuweiheng.base.utils.StringUtil;
import com.ourway.dtos.UserDTO;
import com.ourway.entity.vo.PageQueryVo;
import com.ourway.entity.vo.PageResultDto;
import com.ourway.service.IFeiShuService;
import com.ourway.service.IUserService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author lwh
 * @date 2021/12/03
 */
@RestController
@RequestMapping("/feishu/user")
public class FeiShuUserController {

    private final IFeiShuService IFeiShuService;
    private final IUserService userService;

    public FeiShuUserController(IFeiShuService IFeiShuService, IUserService userService) {
        this.IFeiShuService = IFeiShuService;
        this.userService = userService;
    }

    @PostMapping("/findUser")
    public PageResultDto<UserDTO> findUser(@RequestBody PageQueryVo param) {
        int pageIndex = param.getPageIndex();
        int pageSize = param.getPageSize();
        return IFeiShuService.getUserList(pageIndex, pageSize);
    }

    @GetMapping("/findAll")
    public List<UserDTO> findAllUser() {
        return IFeiShuService.getAllUser();
    }

    @GetMapping("/check/{userNo}/{userId}")
    public R checkUserNo(@PathVariable("userNo") String userNo, @PathVariable("userId") String userId) {
        if (IFeiShuService.checkUserNo(userId, userNo)) {
            return R.error("userNo已存在");
        }
        return R.ok();
    }

    @GetMapping("/check/{userNo}")
    public R checkUserNo(@PathVariable("userNo") String userNo) {
        if (IFeiShuService.checkUserNo(null, userNo)) {
            return R.error("userNo已存在");
        }
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody UserDTO userDTO) {
        if (null == userDTO || StrUtil.isBlank(userDTO.getUserId())) {
            return R.error("Bad Request! RequestParam Is Empty");
        }
        if (!userService.update(userDTO)) {
            return R.error("更新失败");
        }
        return R.ok("更新成功");
    }

    @PostMapping("/create")
    public R create(@RequestBody UserDTO userDTO) {
        if (null == userDTO) {
            return R.error("Bad Request! RequestParam Is Empty");
        }
        if (!userService.create(userDTO)) {
            return R.error("创建失败");
        }
        return R.ok("创建成功");
    }

    @PostMapping("/delete")
    public R delete(@RequestBody UserDTO userDTO) {
        if (null == userDTO || StrUtil.isBlank(userDTO.getUserId())) {
            return R.error("Bad Request! RequestParam Is Empty");
        }
        if (!userService.delete(userDTO)) {
            return R.error("删除失败");
        }
        return R.ok("删除成功");
    }

}
